/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package master.proyectoweb.ejb;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import master.proyectoweb.entities.CustomerOrder;
import master.proyectoweb.entities.OrderedProduct;
import master.proyectoweb.entities.OrderedProductPK;

/**
 *
 * @author joaquin
 */
@Stateless
public class CustomerOrderFacade extends AbstractFacade<CustomerOrder> {
    @PersistenceContext(unitName = "TurnItUpPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public CustomerOrderFacade() {
        super(CustomerOrder.class);
    }

    public void createOrder(CustomerOrder order, List<OrderedProduct> listOrderedProducts) {
        Iterator<OrderedProduct> it;
        OrderedProduct op;
        
        getEntityManager().persist(order);
        
        getEntityManager().flush();
        
        //set the primary key for every ordered product, based on the customer order id and the product id
        it = listOrderedProducts.iterator();
        while(it.hasNext()){
            op = it.next();
            op.setOrderedProductPK(new OrderedProductPK(order.getId(), op.getProduct().getId()));
            
            getEntityManager().persist(op);
        }
    }
    
    
    
}
